/**
* Copyright 2009, Jethro Grassie, Creative Jar Technology Ltd (http://www.cjtech.co.uk)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
**/

package com.cjt.sample.commands;

import com.adobe.cairngorm.commands.Command;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.business.Responder;
import com.adobe.cairngorm.business.HTTPService;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import com.cjt.sample.Model;
import com.cjt.sample.business.Services;

public class GetTimeCommand extends Command, Responder
{
    public override function execute(event:CairngormEvent):Void
    {
        println("Executing GetTimeCommand");
        var service:HTTPService = Services.getInstance().getHTTPService("getTime");
        service.addResponder(this);
        service.start();
    }

    public override function onResult(data:Object):Void
    {
        var stream:InputStream = data as InputStream;
        var model = Model.getInstance();
        var reader:BufferedReader = new BufferedReader(new InputStreamReader(stream));
        model.lastResult = reader.readLine();
    }

    public override function onFault(data:Object):Void
    {
        var stream:InputStream = data as InputStream;
        var model = Model.getInstance();
        var reader:BufferedReader = new BufferedReader(new InputStreamReader(stream));
        model.lastResult = reader.readLine();
    }
}

